﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BusinessObject;

namespace YamahaTuanTrucService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerService" in code, svc and config file together.
    public class CustomerService : ICustomerService
    {
        public KhachHang SaveCustomer(KhachHang kh)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                context.KhachHang.ApplyChanges(kh);
                context.SaveChanges();
                return kh;
            }
        }

        public bool DeleteCustomer(int id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                KhachHang kh = context.KhachHang.SingleOrDefault(k => k.ID == id);
                if (kh != null)
                {
                    context.DeleteObject(kh);
                    int result = context.SaveChanges();
                    return result > 0;
                }
            }
            return false;
        }

        public KhachHang[] GetCustomers()
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                var records = context.KhachHang.Include("Xe").ToArray<KhachHang>();
                return records;
            }
        }

        public KhachHang GetCustomerById(int id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                var kh = context.KhachHang.SingleOrDefault(k => k.ID == id);
                return kh;
            }
        }

        public KhachHang GetCustomerByIdWithBikes(int id)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                var kh = context.KhachHang.Include("Xe").SingleOrDefault(k => k.ID == id);
                return kh;
            }
        }
    }
}
